package com.example.demo.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;

/**
 * @ClassName ApplicationConfiguration
 * @Author lys4134
 * @Date 2021/07/19 10:13:45
 * @Description ApplicationConfiguration
 * @Version 1.0
 **/
@Configuration
@Data
public class ApplicationConfiguration implements WebMvcConfigurer {
    @Value("${multipart.maxFileSize}")
    private String maxFileSize;
    @Value("${multipart.maxRequestSize}")
    private String maxRequestSize;
    @Value("${multipart.enclosure-save-path}")
    private String enclosureSavePath;

    @Autowired
    private AppInterceptor appInterceptor;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();

        //factory.setMaxFileSize(1024);
        //单个文件最大
        factory.setMaxFileSize(DataSize.parse(maxFileSize)); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.parse(maxRequestSize));
        return factory.createMultipartConfig();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        // 注册拦截器
        InterceptorRegistration loginRegistry = registry.addInterceptor(appInterceptor);
        // 拦截路径
        loginRegistry.addPathPatterns("/**");
        // 排除路径
//        loginRegistry.excludePathPatterns("/");
    }
}
